package com.yixun.qfbao.api.service.impl;

import com.yixun.qfbao.api.service.TokenService;
import com.yixun.qfbao.apidto.GetTokenReqDto;
import com.yixun.qfbao.mapper.TokenMapper;
import com.yixun.qfbao.model.OtherCompany;
import com.yixun.qfbao.model.Token;
import com.yixun.qfbao.model.dto.TokenDto;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.dao.OtherCompanyDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.Calendar;
import java.util.List;
import java.util.Random;

@Service
public class TokenServiceImpl implements TokenService {
    private static Logger logger= LoggerFactory.getLogger(TokenServiceImpl.class);
    public static final String KEY_SHA = "SHA";

    @Autowired
    OtherCompanyDao otherCompanyDao;

    @Autowired
    TokenMapper tokenMapper;

    /**
     * v10 调用api  获取token
     * @param dto
     * @return
     */
    @Override
    public Token createToken(GetTokenReqDto dto) {
        //查询 是否在当前关系内
        if(dto == null && dto.getCompanyUuid() != null){
            return null;
        }
        OtherCompany otherCompany = new OtherCompany();
        otherCompany.setIsDeleted(DelFlagEnum.UNDEL.getNo());
        otherCompany.setCompanyUuid(dto.getCompanyUuid());
        otherCompany =  otherCompanyDao.selectBy(otherCompany);
        String token = null;
        if(otherCompany != null){
            token = buildToken(otherCompany.getCompanyUuid());
        }
        //存表
        Token tokenModel = new Token();
        tokenModel.setCompanyuuid(dto.getCompanyUuid());
        tokenModel.setIsDeleted(DelFlagEnum.UNDEL.getNo());
        tokenModel.setStrToken(token);
        Calendar calendar = Calendar.getInstance();
        tokenModel.setCreateTime(calendar.getTime());
        tokenModel.setTimes(108000L);
        calendar.add(Calendar.MINUTE,30);
        tokenModel.setValidTime(calendar.getTime());

        int i = tokenMapper.add(tokenModel);
        if(i > 0){
            return tokenModel;
        }
        return null;

    }

    /**
     * 生成token ,SHA加密数据， companyUuid + 时间 + 随机数
     * @return
     */
    public static String buildToken(String commpanyUuid){

        BigInteger sha =null;
        String token = commpanyUuid + Calendar.getInstance().getTimeInMillis() + new Random().nextInt();
        byte[] inputData = token.getBytes();
        try {
            MessageDigest messageDigest = MessageDigest.getInstance(KEY_SHA);
            messageDigest.update(inputData);
            sha = new BigInteger(messageDigest.digest());
        } catch (Exception e) {
            logger.error("TokenServiceImpl ==> buildToken error: {}", e);}
        return sha.toString(32);
    }

    /**
     * 获取最新的token
     * @param companyUuid
     * @return
     */
    public Token getLastToken(String companyUuid){
        TokenDto dto = new TokenDto();
        dto.setCompanyuuid(companyUuid);
        dto.setIsDeleted(DelFlagEnum.UNDEL.getNo());
        dto.setOrders(" create_time desc");
        List<Token> list = tokenMapper.list(dto,0,1);
        if(list == null || list.size() == 0){
            return null;
        }
        return list.get(0);
    }

    public static void main(String[] args) {
        System.out.println(buildToken("asdfsdfsfsdf"));
    }
}
